/**
 * 
 */
package com.oauth.global.client.ui;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.oauth.components.client.beans.OAuthUrl;
import com.oauth.components.client.rpc.OAuthAsnyCallBack;
import com.oauth.global.client.rpc.GlobalRemoteServiceAsync;

/**
 * @author asanthan
 * 
 */
public class SignInButtonClickHandler implements ClickHandler {

	Long applicationId;

	public SignInButtonClickHandler(Long applicationId) {
		super();
		this.applicationId = applicationId;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event
	 * .dom.client.ClickEvent)
	 */
	@Override
	public void onClick(ClickEvent event) {
		final Button source = (Button) event.getSource();
		source.setEnabled(false);

		GlobalRemoteServiceAsync.INSTANCE.getSignInUrl(applicationId,
				new OAuthAsnyCallBack<OAuthUrl>() {

					@Override
					public void onSuccess(OAuthUrl result) {
						source.setText("Loading...");
						if (result.getOAuthUrl() != null) {
							Window.Location.assign(result.getOAuthUrl());
						}
					}
				});

	}
}
